
<!--数据字典下拉框-->
<template>
    <el-select  v-model="svalue" @change="changeValue" >
      <el-option :label="item.dictName" :value="item.dictCode" :key="item.id" v-for="item in list" ></el-option>
    </el-select>
</template>

<script>
import Http from '../libs/http.js'
export default {
  name: 'DataDictSelect',
  props:["p_value","p_value1","p_name","dataKey"],
  data () {
    return {
      list:[],
      key:this.dataKey,
      svalue:this.p_value
    }
  },
  watch:{
    p_value(v){
      this.svalue= v;
      console.log(this.svalue);
    }
  },
  created(){
    this.getDictall(this.key);
  },

  methods:{
  changeValue(v){
      console.log(v);
      let item = this.getItemByCode(v);
      console.log(item);
      if(item){
        this.$emit('update:p_value',item.dictCode)
        this.$emit('update:p_name',item.dictName)
      }else{
        this.$emit('update:p_value','')
        this.$emit('update:p_name','')
      }
    },
    getDictall(key){
      Http.api.get("dict",{params:{key:key}}).then((res)=>{
        this.list = res.data;
      })
    },
    getItemByCode(code){
      for( let index in this.list){
        if(this.list[index].dictCode == code){
          return this.list[index] ;
        }
      }
    }
  }
}
</script>

